using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text;

namespace FadeJSON
{
    public static class Utility
    {

        private static readonly Dictionary<int, string> tabCache = new Dictionary<int, string>();
        internal static string GetTabspace(this int tab)
        {
            string result = string.Empty;
            if (!tabCache.TryGetValue(tab, out result))
            {
                var builder = new StringBuilder();
                for (int i = 0; i < tab; i++)
                {
                    builder.Append(' ');
                }
                result = builder.ToString();
                tabCache.Add(tab, result);
            }

            return result;
        }

        public static int LineNumber = 1;

        [Conditional("DEBUG")]
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        private static void AddLineNumber(char chr)
        {
            if (chr == '\n')
            {
                LineNumber++;
            }
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static bool IsWhiteChar(this char c)
        {
            AddLineNumber(c);
            return c == ' ' || c == '\t' || c == '\n' || c == '\r';
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static bool IsDigit(this char c)
        {
            return '0' <= c && c <= '9';
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static bool IsDigit1_9(this char c)
        {
            return '1' <= c && c <= '9';
        }
    }
}